<?php
Yii::import('zii.widgets.grid.CGridView');
/**
 * Modifies CGridView:
 * <ul><li>$behaviors properties, enabling the attachment of behaviors in the configuration (a CGridView is a widget);</li>
 * <li>To render only when:
 *   <ul><li>there is no ajax request, or,</li>
 *   <li>there is an ajax request containing the id of the gridview.</li></ul>
 * </li><ul>.
 *
 */
class LyadGridView extends CGridView {
    public $isRender=false;
 
    /**
     * @var array Behaviors to be enabled for the CGridView.
     */
    public $behaviors = array();
 
    /**
     * @var boolean When true (default), calls the parent rendering method to keep the default 'id' numbering.
     *              Can be set to 'false' when all ids are fixed (or of no importance).
     */
    public $callParentRender = true;
 
    public function __construct($owner=null) {
        parent::__construct($owner);
    }
 
    /**
     * Extends CGridview with:
     * <ul><li>Check if this CGridView is to be rendered</li>
     * <li>Attc
     * (non-PHPdoc)
     * @see CGridView::init()
     */
    public function init() {
        /* @var $request CHttpRequest */
        $request = Yii::app()->request;
        $ajax    = $request->getParam($this->ajaxVar,null);
        if($ajax!==null) {
            $this->isRender |= array_search($this->getId(),explode(',',$ajax))!==false;
        } else {
            $this->isRender = true;
        }
        $this->attachBehaviors($this->behaviors);
        return parent::init();
    }
 
    /**
     * Default 'run' method changed to render only when requests.
     *
     * The parent render method is called in this implementation to make sure that id sequences are the same.
     *
     *
     * (non-PHPdoc)
     * @see CBaseListView::run()
     */
    public function run() {
        if($this->isRender) {
            return parent::run();
        } else {
            if(!$this->callParentRender) {
                return null;
            } else {
                ob_start();
                ob_implicit_flush(false);
                parent::run();
                ob_get_clean();
                return null;
            }
        }
    }
 
 
    /**
     * Replaces default render method to include any rendering method (including methods provided by behaviors).
     *
     * The original method used 'method_exists' which does not work for methods provided by behaviors.
     *
     * (non-PHPdoc)
     * @see CBaseListView::renderSection()
     */
    public function renderSection($matches) {
        parent::renderSection($matches);
        $method='render'.ucfirst($matches[1]);
        try {
            $this->$method();
            $html=ob_get_contents();
            ob_clean();
            return $html;
        } catch (Exception $e) {
            // The exception should never occur because only existing sections should be rendered.
            throw $e;
        }
        return $matches[0];
    }
}